<!DOCTYPE html>
<html>
<head>
    <title>修改医生--中软高科-2015</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap.css"/>
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap-responsive.css"/>
    <link rel="stylesheet" type="text/css" href="../Css/style.css"/>
    <script type="text/javascript" src="../Js/jquery.js"></script>
    <script type="text/javascript" src="../Js/jquery.sorted.js"></script>
    <script type="text/javascript" src="../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../Js/ckform.js"></script>
    <script type="text/javascript" src="../Js/common.js"></script>
    <script type="text/javascript" src="../Js/ckeditor/ckeditor.js"></script>
    <script type="text/javascript" src="../Js/webUtil.js"></script>
    <style type="text/css">
        body {
            padding-bottom: 40px;
        }

        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }


    </style>
    <script type="text/javascript">
        $(function () {
            $('#backid').click(function () {
                window.location.href = "index.html";
            });
        });
    </script>
</head>
<body>
<form action="index.html" method="post" class="definewidth m20">
    <table class="table table-bordered table-hover definewidth m10">

        <tr>
            <td width="10%" class="tableleft">编号</td>
            <td><input type="text" name="id" value="程俊" readonly  unselectable="on"/></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">姓名</td>
            <td><input type="text" name="name" value="程俊"/></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">身份证号</td>
            <td><input type="text" name="idNumber" value=""/></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">手机</td>
            <td><input type="text" name="phone" value="15343457897"/></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">座机</td>
            <td><input type="text" name="telephone" value="01088586875"/></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">性别</td>
            <td>
                <input name="sex"  readonly  unselectable="on"/>
            </td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">出生年月</td>
            <td><input type="date" name="birthday" value="1986-09-06" readonly  unselectable="on"/></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">年龄</td>
            <td><input type="text" name="age" value="28岁" readonly  unselectable="on"/></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">电子邮箱</td>
            <td><input type="text" name="email" value="chengjun@msn.com.cn"/></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">所属科室</td>
            <td>
                <select name="departmentId">
                </select>
            </td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">学历</td>
            <td>
                <select name="degreeId">
                </select>
            </td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">备注</td>
            <td><textarea name="comment">此人表现优异工作非常的积极！</textarea></td>
        </tr>
        <tr>
            <td colspan="2">
                <center>
                    <button class="btn btn-primary" type="button" onclick="update()">保存</button> &nbsp;&nbsp;
                    <button type="button" class="btn btn-success" name="backid" onclick="back()">返回列表
                    </button>
                </center>
            </td>
        </tr>
    </table>

</form>
</body>
</html>
<script>
    function updateByNumber(){
        let idNumber = $("input[name='idNumber']").val();
        if(!idNumber.isIdNumber()){
            alert("请输入正确的身份证号!")
            return false;
        }
        else{
            let year=idNumber.substr(6,4);
            let month=idNumber.substr(10,2);
            let day=idNumber.substr(12,2);
            let birthday=year+"-"+month+"-"+day;
            $("input[name='birthday']").val(year+"-"+month+"-"+day)

            let birthday_date = +new Date(birthday);
            $("input[name='age']").val(~~((Date.now() - birthday_date) / (31557600000)))

            let sex="女";
            if(idNumber[16]%2==1)
                sex="男";
            $("input[name='sex']").val(sex)
            return true;
        }
    }

    function back() {
        window.location.href = "index.html";
    }

    function update() {
        let name = $("input[name='name']").val();
        let phone = $("input[name='phone']").val();
        let telephone = $("input[name='telephone']").val();

        let email = $("input[name='email']").val();

        if (!name.isName()) {
            alert("请输入正确的姓名!");
            return;
        }

        if(!updateByNumber())
            return;

        if (!phone.isPhone()) {
            alert("请输入正确的手机号码!");
            return;
        }
        if (!telephone.isTelephone()) {
            alert("请输入正确的座机号码!");
            return;
        }
        if (!email.isEmail()) {
            alert("请输入正确的邮箱!");
            return;
        }

        let url = document.location.toString();
        let id = url.GetValue("id");
        $.ajax({
            type: "POST",
            url: "/doctors/" + id,
            data: $("form").serialize() + "&_method=PUT",
            success: function (vo) {
                if (vo.code == 200) {
                    alert(vo.obj);
                    window.location.href = "index.html"
                } else
                    alert(vo.msg);
            }
        })
    }

    function loadData() {
        let url = document.location.toString();
        let id = url.GetValue("id");
        $.ajax({
            type: "GET",
            url: "/degree/list",
            success: function (vo) {
                if (vo.code == 200) {
                    let list = vo.list;
                    let str = "";
                    for (let i = 0; i < list.length; i++) {
                        str += "<option value='" + list[i].id + "'>" + list[i].name + "</option>";
                    }
                    $("select[name='degreeId']").html(str);
                } else
                    alert(vo.msg);
            }
        })

        $.ajax({
            type: "GET",
            url: "/department/list",
            success: function (vo) {
                if (vo.code == 200) {
                    let list = vo.list;
                    let str = "";
                    for (let i = 0; i < list.length; i++) {
                        str += "<option value='" + list[i].id + "'>" + list[i].name + "</option>";
                    }
                    $("select[name='departmentId']").html(str);
                } else
                    alert(vo.msg);
            }
        })

        if (id != null) {
            $.ajax({
                type: "GET",
                url: "/doctors/" + id,
                success: function (vo) {
                    if (vo.code == 200) {
                        let obj = vo.obj;
                        $("input[name='name']").val(obj.name);
                        $("input[name='id']").val(obj.id);
                        $("input[name='phone']").val(obj.phone);
                        $("input[name='telephone']").val(obj.telephone);
                        $("input[name='email']").val(obj.email);
                        $("input[name='age']").val(obj.age);
                        $("input[name='idNumber']").val(obj.idNumber);
                        $("input[name='birthday']").val(obj.birthday);
                        $("textarea[name='comment']").val(obj.comment);
                        $("input[name='sex']").val(obj.sex).prop('checked');
                        $("select[name='degreeId']").val(obj.degree.id);
                        $("select[name='departmentId']").val(obj.department.id);
                        $("input[name='state']").val(obj.state);
                        $("input[name='inTime']").val(obj.inTime);
                    } else {
                        alert(vo.msg);
                    }
                }
            })
        } else {
            alert("非法ID");
        }
    }

    $(function () {
        loadData();
        $("input[name='idNumber']").blur(()=>updateByNumber());
    })

</script>